import 'server-only' //代表仅服务端可用
import Joi, { number } from 'joi';
import resFn from '@/app/api/util/res'
import { NextRequest } from 'next/server';
import { logError, logSpecial } from '@/app/api/util/io/log';
import { getBody, judge } from '@/app/api/util/res/handle';
import otherRequest from '@/app/api/util/other/otherRequest';
import { numberJoi, stringJoi } from '@/app/api/util/Joi';
import { RejectData, ResCode } from '@/app/api/util/res/code';
import { uploadReq } from '@/types/api/generate';
export const dynamic = 'force-dynamic';

/**上传文件 接口*/
export async function POST(request: NextRequest) {
    try {
        const { type, name,path, file } = await judge(await getBody<uploadReq>(request), { type: stringJoi(), name: stringJoi(),path:stringJoi(),file:stringJoi() })
        const res = await otherRequest.postByJsonData<string>('http://172.30.10.139:8003/public_resource/upload',undefined,{
          type,
          name,
          path,
          file:file.split(';')[1].slice(7)
        })
        // logSpecial("@json_data",param.substring(0,500),"...超过500,已截断")
        logSpecial('@res',res)
        if (res.code !== 0) {
            throw new RejectData(ResCode.FAIL_REQUEST, '获取结果失败')
        }
        return resFn.success<string>(res.data);
    } catch (error: any) {
        logError('上传文件 接口', error)
        return resFn.errorWithReject(error);
    }
}